#Region " Imports "

Imports System.Data
Imports BlazeApps.Library.Forum

#End Region

Partial Class Usercontrols_Forum_Topic
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

	Private ForumCheck As New ForumChecks
    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

    Protected Property BoardGuid() As Guid
        Get
            Return GetGuidFromVS("BoardID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("BoardID", value)
        End Set
    End Property

    Protected Property CategoryGuid() As Guid
        Get
            Return GetGuidFromVS("CategoryID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("CategoryID", value)
        End Set
    End Property

    Protected Property ForumGuid() As Guid
        Get
            Return GetGuidFromVS("ForumID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ForumID", value)
        End Set
    End Property

    Protected Property TopicGuid() As Guid
        Get
            Return GetGuidFromVS("TopicID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("TopicID", value)
        End Set
    End Property

    Protected Property BoardName() As String
        Get
            Return GetStringFromVS("BoardName")
        End Get
        Set(ByVal value As String)
            ViewState.Add("BoardName", value)
        End Set
    End Property

    Protected Property CategoryName() As String
        Get
            Return GetStringFromVS("CategoryName")
        End Get
        Set(ByVal value As String)
            ViewState.Add("CategoryName", value)
        End Set
    End Property

    Protected Property ForumName() As String
        Get
            Return GetStringFromVS("ForumName")
        End Get
        Set(ByVal value As String)
            ViewState.Add("ForumName", value)
        End Set
    End Property

    Protected Property TopicName() As String
        Get
            Return GetStringFromVS("TopicName")
        End Get
        Set(ByVal value As String)
            ViewState.Add("TopicName", value)
        End Set
    End Property

    Protected Property ForumLocked() As Boolean
        Get
            Return GetBooleanFromVS("Locked")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("Locked", value)
        End Set
	End Property

	Protected Property ForumModerated() As Boolean
		Get
			Return GetBooleanFromVS("ForumModerated")
		End Get
		Set(ByVal value As Boolean)
			ViewState.Add("ForumModerated", value)
		End Set
	End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Topics")

        If ShowItem(ForumGuid) = True Then
            GetQueryStringValues()

            If Me.Page.IsPostBack = False Then

				ForumModerated = ForumCheck.IsForumModerated(ForumGuid, Guid.Empty)

                'Set Default Sort Order and Direction
                Me.sortCriteria = "Topicname"
                Me.sortDir = "asc"

                LoadPage()
            End If

            ConfigBaseNavCtrls()

            If Me.Page.IsPostBack = False Then
                BuildBreadCrumbs()
                BindDataItems()
            End If

            If IsUserAuthenticated() Then
                Me.uxNewTopicButton.Visible = (Not ForumLocked) Or IsModerator()
            Else
                Me.uxNewTopicButton.Visible = False
			End If

			If ForumModerated Then
				Me.uxStatusLabel.Text = Resources.Resource.ForumModerated
			End If
        Else
            Me.uxTopicPanel.Visible = False
            Me.uxTopicPanel.Enabled = False
        End If

    End Sub

#End Region

#Region " Save Data "

#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Private Sub BuildBreadCrumbs()

        Dim List As New ForumList
        Dim DT As New DataTable

        DT = List.GetBreadCrumbLinks(ForumGuid)

        With Me.uxBreadCrumbsRepeater
            .DataSource = DT
            .DataBind()
        End With

        Dim DR As DataRow

        For Each DR In DT.Rows
            ForumName = DR("forumname").ToString
        Next

    End Sub

    Public Overloads Overrides Sub BindDataItems()

        Dim List As New TopicList
        Dim DT As New DataTable
        Dim DV As New DataView

        DT = List.GetTopics(ForumGuid)

        Me.RecordCount = DT.Rows.Count

        DV.Table = DT

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        With Me.uxTopicRepeater
            .DataSource = Me.DataPage
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub LoadBoards(ByVal sender As Object, ByVal e As CommandEventArgs)
        Response.Redirect(String.Format("forum.aspx?pid=for_board"))
    End Sub

    Protected Sub LoadCategories(ByVal sender As Object, ByVal e As CommandEventArgs)
        Response.Redirect(String.Format("forum.aspx?pid=for_category&categoryid={0}", e.CommandArgument.ToString))
    End Sub

    Protected Sub LoadPosts(ByVal sender As Object, ByVal e As CommandEventArgs)
        Response.Redirect(String.Format("forum.aspx?pid=for_post&topicid={0}&forumid={1}", e.CommandArgument.ToString, ForumGuid))
    End Sub

    Protected Sub LoadForums(ByVal sender As Object, ByVal e As CommandEventArgs)
        Response.Redirect(String.Format("forum.aspx?pid=for_topic&forumid={0}", e.CommandArgument.ToString))
    End Sub

    Protected Sub AddTopic(ByVal sender As Object, ByVal e As CommandEventArgs)
        Response.Redirect(String.Format("forum.aspx?pid=for_addtopic&forumid={0}", ForumGuid))
    End Sub

    Protected Sub uxTopicRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles uxTopicRepeater.ItemDataBound
        Dim item As RepeaterItem = e.Item
        Dim Icon As Image = CType(item.FindControl("uxStatusImage"), Image)

        If (item.ItemType = ListItemType.Item) OrElse (item.ItemType = ListItemType.AlternatingItem) Then
            If ShowItem(ConvertToGuid(e.Item.DataItem("ForumID").ToString)) = True Then
                If CType(item.DataItem("locked").ToString, Boolean) Then
                    Icon.ImageUrl = String.Format("~/App_Themes/{0}/images/icons/topic_lock.gif", Page.Theme.ToString)
                    ForumLocked = True
                Else
                    Icon.ImageUrl = String.Format("~/App_Themes/{0}/images/icons/topic.gif", Page.Theme.ToString)
                    ForumLocked = False
                End If
            Else
                item.Visible = False
            End If
        End If
    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        ForumGuid = GetGuidFromQueryString("forumid")
    End Sub

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

#End Region

End Class
